﻿Ext.define("PSI.CntOwner.MainForm", {
    createToolButtons: function () {
        var me = this;
        var result = [];
        result.push("-");
        result.push({ text: "新增箱属", iconCls: "psi-button-add", handler: me.onAdd, scope: me });
        result.push("-");
        result.push({ text: "编辑箱属", iconCls: "psi-button-edit", handler: me.onEdit, scope: me });
        result.push("-");
        result.push({ text: "删除箱属", iconCls: "psi-button-delete", handler: me.onDelete, scope: me });
        result.push("-");
        result.push({
            text: "关闭", iconCls: "psi-button-exit", handler: function () {
                location.replace("/");
            }
        });
        return result;
    },

    createUI: function () {
        var me = this;

        var panel = Ext.create("Ext.panel.Panel", {
            border: 0,
            layout: "border",
            title: "箱属",
            tbar: me.createToolButtons(),
            items: [{
                region: "center", xtype: "container", layout: "fit", border: false,
                items: [me.getGrid()]
            }
            ]
        });

        me.panel = panel;

        me.getGrid().on("itemdblclick", me.onEdit, me);

        me.refreshGrid();
    },

    getMainPanel: function () {
        return this.panel;
    },

    getGrid: function () {
        var me = this;
        if (me.__grid == null) {
            me.__grid = me.createGrid();
        }

        return me.__grid;
    },

    createGrid: function () {
        var modelName = "PSICntOwnerModel";
        Ext.define(modelName, {
            extend: "Ext.data.Model",
            fields: ["id", "name", "pinyin"]
        });

        return Ext.create("Ext.grid.Panel", {
            viewConfig: { enableTextSelection: true },
            columnLines: true,
            columns: [
                     Ext.create("Ext.grid.RowNumberer", { text: "", width: 30 }),
                    { header: "箱属", dataIndex: "name", menuDisabled: true },
                    { header: "助记码", dataIndex: "pinyin", menuDisabled: true }
            ],
            store: Ext.create("Ext.data.Store", {
                autoLoad: false,
                model: modelName,
                data: []
            })
        });
    },

    refreshGrid: function (selectedId) {
        var me = this;
        var grid = me.getGrid();
        var store = grid.getStore();
        var items = grid.getSelectionModel().getSelection();
        if (selectedId == null && items && items.length > 0) {
            selectedId = items[0].getData().id;
        }
        var el = grid.getEl() || Ext.getBody();
        el.mask("数据加载中...");
        Ext.Ajax.request({
            url: "/CntOwner/GetListData",
            method: "POST",
            callback: function (options, success, response) {
                store.removeAll();

                if (success) {
                    var data = Ext.JSON.decode(response.responseText);
                    store.add(data);

                    if (selectedId) {
                        for (var i = 0; i < store.getCount() ; i++) {
                            var m = store.getAt(i);
                            if (m.get("id") == selectedId) {
                                grid.getSelectionModel().select(m);
                                break;
                            }
                        }
                    } else {
                        if (data.length > 0) {
                            grid.getSelectionModel().select(0);
                        }
                    }
                } else {
                    PSI.MsgBox.showInfo("网络错误");
                }

                el.unmask();
            }
        });
    },

    onAdd: function () {
        var form = Ext.create("PSI.CntOwner.EditForm");
        form.show(this,null,"self");
    },

    onEdit: function () {
        var me = this;
        var items = me.getGrid().getSelectionModel().getSelection();
        if (items == null || items.length != 1) {
            PSI.MsgBox.showInfo("请选择要编辑的箱属");

            return;
        }

        var entity = items[0].getData();

        var form = Ext.create("PSI.CntOwner.EditForm");
        form.show(this, entity,null);
    },

    onDelete: function () {
        var me = this;
        var items = me.getGrid().getSelectionModel().getSelection();
        if (items == null || items.length != 1) {
            PSI.MsgBox.showInfo("请选择要删除的箱属");

            return;
        }


        var entity = items[0].getData();

        var funcSumbit = function () {
            Ext.getBody().mask("正在删除中...");
            Ext.Ajax.request({
                url: "/CntOwner/Delete",
                method: "POST",
                params: { id: entity.id },
                callback: function (options, success, response) {
                    Ext.getBody().unmask();

                    if (success) {
                        var data = Ext.JSON.decode(response.responseText);
                        if (data.success) {
                            PSI.MsgBox.showInfo("成功完成删除操作", function () {
                                me.refreshGrid();
                            });
                        } else {
                            PSI.MsgBox.showInfo(data.msg);
                        }
                    } else {
                        PSI.MsgBox.showInfo("网络错误");
                    }
                }
            });
        };

        PSI.MsgBox.confirm("请确认是否删除当前箱属?", funcSumbit);

    }
});